#include <string>
#include <iostream>
#include <map> 
#include <list>
#include <fstream>
#include <algorithm>
#include <boost/unordered_map.hpp>
#include <boost/foreach.hpp>
#include <time.h>
#include <sys/time.h>
using namespace std;
typedef boost::unordered_map<std::string, int> dict;
typedef boost::unordered_map<std::string, dict> long_dict;
string path="sigdb_top/";
string outpath="res_top/";
const int thres=238;
long_dict dict_list;

int find_share(string src_f,string dst_f,int flag=0)
{
 
    dict src,dst;
    if (flag==0)
    {
        ifstream in1((path+src_f).c_str());
        ifstream in2((path+dst_f).c_str());
        string s;
    
        while(getline(in1,s)){
            int i=s.find(',');
            string key=s.substr(0,i);
            string value=s.substr(i+1,s.size());
        //cout << key<<","<<value << endl;
            src[key.c_str()]=atoi(value.c_str());

        }
        in1.close();
        while(getline(in2,s)){
            int i=s.find(',');
            string key=s.substr(0,i);
            string value=s.substr(i+1,s.size());
        //cout << key<<","<<value << endl;
            dst[key.c_str()]=atoi(value.c_str());

        }
        in2.close();

        dict_list[dst_f]=dst;
    }
    else
    {
        src=dict_list.at(src_f);
        dst=dict_list.at(dst_f);
    }
    int shared=0;
    BOOST_FOREACH(dict::value_type i, src) {
        if(dst.find(i.first) != dst.end())
        {
            shared=shared+min(src.at(i.first),dst.at(i.first));
        }
    }
   
    if (shared >= thres)
{
	cout<<src_f<<endl;
        return 1;
}
    else
        return 0;
}

void dump_res(string target,dict res)
{   
    ofstream fout((outpath+target).c_str());
    BOOST_FOREACH(dict::value_type i, res) {  
        fout<<i.first<<"\n";
    } 
    fout.close();
}



int main(int argc, char* argv[])
{
    std::map<int,string> filelist;
   	 
    ifstream in(argv[1]);
    string s;
    int lsize=0;

    while(getline(in,s)){
        //filelist[s]=0;
        filelist.insert ( std::pair<int,string>(lsize,s) );
        lsize=lsize+1;
    }
    in.close();

    //first process
    string target=(*filelist.begin()).second;
    dict result;
    std::list<int> mylist;
   
    for (std::map<int, string>::iterator iter=filelist.begin( ); iter != filelist.end( ); ++iter)
    {
        string tmp=(*iter).second;
        int res=find_share(target,tmp);
        if (res==1)
        {
            mylist.push_front ((*iter).first);
            result[tmp]=1;
        }
    }
     //if (mylist.size()==0)
       //          mylist.push_front (target);
   
    for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    {
        filelist.erase(*it);
    }
   
    std::cout << "filelist.size() is " << filelist.size() << '\n';
    std::cout << "full dict size " <<dict_list.size()<< endl;
    dump_res(target,result);

    //second process
    while(1)
    {
        if (filelist.size()==0)
        {
            break;
        }
        string target=(*filelist.begin()).second;
        dict result;
        std::list<int> mylist;
	cout<<"tag is "<<target<<endl;
   
        for (std::map<int, string>::iterator iter=filelist.begin( ); iter != filelist.end( ); ++iter)
        {
            string tmp=(*iter).second;
	    if (target!=tmp){
		//cout<<target<<","<<tmp<<endl;
		//continue;
            //}
            int res=find_share(target,tmp,1);
            if (res==1)
            {
                mylist.push_front ((*iter).first);
                result[tmp]=1;

            }
	   }
        }
	//if (mylist.size()==0)
	//{
		 mylist.push_front ((*filelist.begin()).first);
		 result[target]=1;
   	//}
        for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
        {
            
            filelist.erase(*it);
        }
   	cout << "find items "<< mylist.size()<< "::" <<result.size() <<endl;
        dump_res(target,result);

        std::cout << "filelist.size() is " << filelist.size() << '\n';

    }
    return 0;


}
